package com.wolf.controller;

import com.wolf.pojo.Wl_order;
import com.wolf.pojo.Wl_order_record;
import com.wolf.service.Wl_orderService;
import com.wolf.service.Wl_order_recordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class Wl_orderController {

    private Wl_orderService wl_orderService;
    private Wl_order_recordService wl_order_recordService;
    @Autowired
    public void setWl_order_recordService(Wl_order_recordService wl_order_recordService) {
        this.wl_order_recordService = wl_order_recordService;
    }

    @Autowired
    public void setWl_orderService(Wl_orderService wl_orderService) {
        this.wl_orderService = wl_orderService;
    }

    /**
     * 查询所有
     * @param model
     * @return
     */
    @RequestMapping(value = "get_wl_order_All", method = RequestMethod.GET)
    public String getAll(Model model) {
        model.addAttribute("wl_orader_List", wl_orderService.getAll());
        return "table_Wl_order";//spinners.html
    }
    @ResponseBody
    @RequestMapping(value="updateOrder_logistics")
    public String updateOrder_logistics(String id,String logistics)
    {
        Wl_order wl = wl_orderService.selectByPrimaryKey(id);
        wl.setLogisticscode(logistics);
        int rel =wl_orderService.updateByPrimaryKey(wl);
        return ""+(rel==1);
    }

    /**
     * 获得一个对象的信息用于修改
     * @param id
     * @return
     */
    @RequestMapping(value = "get_wl_order_Object", method = RequestMethod.GET)
    public String getObject(ModelMap modelMap,
                            @RequestParam(value = "origin",required = false,defaultValue = "get_wl_order_All")
                                    String origin,
                            @RequestParam(value = "orderid", required = true, defaultValue = " ")
                                    String id) {
        Wl_order info=null;
        //没有带参数则跳出
        if (!id.equals("")) {
            //查询不到内容则跳出
            if ((info = wl_orderService.selectByPrimaryKey(id)) == null)
                return origin;
            else {
                modelMap.addAttribute("orderInfo",info);
                return "wl_order_show"; //此处条正确页面 *
//                return "wl_commodity_brand_update";
            }
        } else
            return origin;
    }

    /**
     * 修改一个对象
     * @param order
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "modify_wl_order", method = RequestMethod.POST)
    public String modifyOrder( Wl_order order) {
        int rel=0;
        if(order!=null)
            rel = wl_orderService.updateByPrimaryKey(order);
        return (rel == 1) + "";
    }
    /**
     * 获得一个对象的信息用于修改Status
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "get_wl_order_Object_Status", method = RequestMethod.GET)
    public String getObjectStatus(        Model model,
                                    @RequestParam(value = "orderid", required = true, defaultValue = " ")
                                            String id) {
        Object info=null;
        //没有带参数则跳出
        if (!id.equals(" ")) {
            //查询不到内容则跳出
            if ((info = wl_orderService.selectByPrimaryKey(id)) == null)
                return "get_wl_order_All_status";
            else {
                model.addAttribute("orderInfo", info);
                return "wl_order_status_show"; //此处条正确页面 *
            }
        } else
            return "get_wl_order_All_status";
    }

    /**
     * 修改一个对象
     * @param order
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "modify_wl_order_Status", method = RequestMethod.POST)
    public String modifyOrderStatus(
            Wl_order order) {
        int rel=0;
        if(order!=null)
            rel = wl_orderService.updateByPrimaryKey(order);
        return (rel == 1) + "";
    }

    /**
     * 获得一个对象的信息用于修改Reminder
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "get_wl_order_Object_Reminder", method = RequestMethod.GET)
    public String getObjectReminder(        Model model,
                                    @RequestParam(value = "orderid", required = true, defaultValue = " ") String id) {
        Object info=null;
        //没有带参数则跳出
        if (!id.equals(" ")) {
            //查询不到内容则跳出
            if ((info = wl_orderService.selectByPrimaryKey(id)) == null)
                return "get_wl_order_All_Reminder";
            else {
                model.addAttribute("orderInfo", info);
                return "wl_order_show_Reminder"; //此处条正确页面 *
            }
        } else
            return "get_wl_order_All_Reminder";
    }

    /**
     * 删除一个对象
     * @param id
     * @return
     */
    @RequestMapping(value = "remove_wl_order", method = RequestMethod.GET)
    public String remove(
            @RequestParam(value = "source" , required = true , defaultValue = "get_wl_order_All") String source,
            @RequestParam(value = "orderid" , required = true, defaultValue = " ") String id) {
        //判断是否传入
        if (!id.equals(" ")) {
            //查询验证其是否存在数据库 避免无效请求
            Wl_order info = wl_orderService.selectByPrimaryKey(id);
            if (info != null) {
                info.setStatus(1);
                int rel = wl_orderService.updateByPrimaryKey(info);
            }
        }
        return "redirect:"+source;
    }

    /**
     * 代发货订单查询
     * @return table_Wl_order_status.html
     */
    @RequestMapping(value = "get_wl_order_All_status", method = RequestMethod.GET)
    public String getOrderAllStatus(Model model) {
        model.addAttribute("orader_status_List", wl_orderService.getAllstatus());
        wl_orderService.getAllstatus();
        return "table_Wl_order_status";//跳頁面s table_Wl_order_status.html
    }

    /**
     * 模糊查询
     * @param model
     * @param beginTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "get_Wl_Order_All_Dim",method = RequestMethod.GET)
    public String getWl_Order_All_Dim(Model model,
                                      @RequestParam(value = "beginTime",required = true)String beginTime ,
                                      @RequestParam(value = "endTime",required = true)String endTime)
    {
        List<Wl_order> lis = wl_orderService.getAllDim(beginTime,endTime);
        model.addAttribute("wl_orader_List",lis);
        return "table_Wl_order";
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 催单信息
     * @param model
     * @return
     */
    @RequestMapping(value = "get_wl_order_All_Reminder", method = RequestMethod.GET)
    public String getOrderAllReminder(Model model) {
        model.addAttribute("orader_reminder_List", wl_orderService.getAllUserReminder());
        return "table_Wl_order_Reminder";//table_Wl_order_Reminder.html
    }


    @RequestMapping(value = "get_reder_record",method = RequestMethod.GET)
    public String getRecord(
            @RequestParam(value = "id" , defaultValue = "",required = true)String id,
            ModelMap modelMap
    ) {
        List<Wl_order_record> wl_order_record = null;
        Wl_order wl_order=null;
        if(
                (wl_order=wl_orderService.selectByPrimaryKey(id))!=null&&
                (wl_order_record=wl_order_recordService.getAll(wl_order.getOrderno()))!=null
            ){
            modelMap.put("order_record",wl_order_record);
            modelMap.put("orderInfo",wl_order);
            return "wl_order_show_record";
        }else
            return "redirect:get_wl_order_All";
    }
}
